package com.dinggao.crossgo.module.roadbook.adapter;

import android.content.ContentResolver;
import android.content.res.Resources;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dinggao.crossgo.R;
import com.dinggao.crossgo.core.observer.EventIntet;
import com.dinggao.crossgo.core.observer.EventMsg;
import com.dinggao.crossgo.core.util.LogUtils;
import com.dinggao.crossgo.module.roadbook.data.UriBean;

import java.util.List;

/**
 * created by mapingan on 2018/7/31.
 */
public class ReleaseAdapter extends BaseQuickAdapter<UriBean, BaseViewHolder> {


    public ReleaseAdapter(int layoutResId, @Nullable List<UriBean> data) {
        super(R.layout.item_uri, data);
    }


    @Override
    protected void convert(final BaseViewHolder helper, final UriBean item) {

        Glide.with(mContext).load(item.getUri()).into((ImageView) helper.getView(R.id.img));


        if (item.getTag()) {
            helper.getView(R.id.del_icon).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.del_icon).setVisibility(View.GONE);
        }


        helper.getView(R.id.del_icon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


               if (getData().size()==9&&getData().get(8).getTag()){
                   EventIntet.getDefault().post(new EventMsg("ReleaseAdapter--delete",helper.getLayoutPosition(), String.valueOf(getClass().getName())));

                   getData().remove(helper.getLayoutPosition());
//                   remove(helper.getLayoutPosition());
//                   LogUtils.e("adad",getData().size()+"ass");
//                   //删除动画
//                   notifyItemRemoved(helper.getLayoutPosition());
                   Resources r = mContext.getResources();
                   Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                           + r.getResourcePackageName(R.drawable.road_publish_photo_add) + "/"
                           + r.getResourceTypeName(R.drawable.road_publish_photo_add) + "/"
                           + r.getResourceEntryName(R.drawable.road_publish_photo_add));
                   UriBean uriBean=new UriBean();
                   uriBean.setUri(uri);
                   uriBean.setTag(false);
                   addData(uriBean);
                   LogUtils.e("adad",getData().size()+"ass");
                   notifyDataSetChanged();

               }else {
                   EventIntet.getDefault().post(new EventMsg("ReleaseAdapter--delete",helper.getLayoutPosition(), String.valueOf(getClass().getName())));

                   remove(helper.getLayoutPosition());
                   //删除动画
                   notifyItemRemoved(helper.getLayoutPosition());
                   notifyDataSetChanged();
               }




            }
        });




    }
}
